<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/9/4
 * Time: 17:51
 * Comment: '服务日志记录中间件'
 */

namespace App\Http\Middleware;

use App\Common\Tools;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Closure;

class ApiLogMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $data['start_time'] = Tools::getMSec();

        $response = $next($request);

        //  上传文件类接口忽略
        if(strpos($request->path(),"upload")
            || strpos($request->path(),"import")) {
            return $response;
        }

        // 请求参数
        $decryptData = $request->except(['file']);
        // 返回参数
        $responseContent = $response->getContent();
        $data['api_url'] = $request->getRequestUri();
        $data['remote_ip'] = $request->getClientIp();
        $data['params'] = $decryptData;
        $data['response'] = $responseContent;
        $data['httpCode'] = 200;
        $data['end_time'] = Tools::getMsec();
        $data['run_time'] = $data['end_time'] - $data['start_time'];
        Log::info("[记录日志内容]".json_encode($data,JSON_UNESCAPED_UNICODE));
        return $response;
    }
}
